enum ControlPadType {
    TouchScreen,
    Keyboard
}

export default class ControlPad extends Laya.Script {
    private control: Laya.Sprite;
    private controlDirection: Laya.Sprite;
    public angle: number;
    public key1Pressed: boolean = false;
    public key2Pressed: boolean = false;
    public key3Pressed: boolean = false;
    public direction: number = -4;
    public directionOffset: number = 0;

    private key1: Laya.Button;
    private key2: Laya.Button;
    private key3: Laya.Button;
    private key4: Laya.Button;

    private isMoving: boolean;
    private touchId: number;
    private centerX: number;
    private centerY: number;
    private _visible: boolean;
    public get visible(): boolean {
        return this._visible;
    }
    public set visible(value: boolean) {
        this._visible = value;
        if (this.owner) {
            var sprite = this.owner as Laya.Sprite;
            sprite.visible = value;
            if (value == false) {
                Laya.timer.clear(this, this.getKeyboardPressed);
                this.direction = -4;
                this.key1Pressed = false;
                this.key2Pressed = false;
                this.key3Pressed = false;
            } else {
                Laya.timer.clear(this, this.getKeyboardPressed);
                if (this.controlPadType === ControlPadType.Keyboard) {
                    Laya.timer.frameLoop(1, this, this.getKeyboardPressed);
                }
            }
        }
    }

    public controlPadType: ControlPadType = ControlPadType.TouchScreen;

    constructor(sceneUrl?: string) {
        super();

        // this.control = new Laya.Sprite();
        // this.control.width = 200;
        // this.control.height= 200;
        // this.control.graphics.drawCircle(0,0,0, "#aaa");
        // this.controlDirection = new Laya.Sprite();
        // this.controlDirection.width = 80;
        // this.controlDirection.height= 80;
        // this.controlDirection.graphics.drawCircle(0,0,0, "#333");

        // Laya.stage.screenMode = "none";
        // Laya.stage.alignV = "middle";
        // Laya.stage.alignH = "center";

        // if (Laya.Browser.onPC) {
        //     Laya.stage.scaleMode = "showall";
        // } else {
        //     Laya.stage.scaleMode = "custom";
        //     // Laya.stage.scaleMode = "fixedwidth";
        // }

        // if (sceneUrl) {
        //     Laya.Scene.load(sceneUrl, Laya.Handler.create(this, function (scene: Laya.Scene) {
        //         //(Laya.stage.scene as Laya.Scene).addComponent(this);
        //         scene.addComponent(this);
        //         scene.name = "controlPad";
        //     }));
        // }
    }

    onAwake() {
        this.control = this.owner.getChildByName("control_pad") as Laya.Sprite;
        this.controlDirection = this.owner.getChildByName("control_point") as Laya.Sprite;
        this.centerX = this.controlDirection.x;
        this.centerY = this.controlDirection.y;
        let controlPadKeys: Laya.Sprite = this.owner.getChildByName("control_pad_keys") as Laya.Sprite;
        this.key1 = controlPadKeys.getChildByName("control_key1") as Laya.Button;
        this.key2 = controlPadKeys.getChildByName("control_key2") as Laya.Button;
        this.key3 = controlPadKeys.getChildByName("control_key3") as Laya.Button;

        if (this.controlPadType === ControlPadType.TouchScreen) {
            Laya.stage.on(Laya.Event.MOUSE_DOWN, this, function (e: Laya.Event) {
                if (Laya.stage.mouseX > this.control.x &&
                    Laya.stage.mouseX < this.control.x + this.control.width &&
                    Laya.stage.mouseY > this.control.y &&
                    Laya.stage.mouseY < this.control.y + this.control.height) {
                    //this.centerX = Laya.stage.mouseX;
                    //this.centerY = Laya.stage.mouseY;
                    this.isMoving = true;
                    this.touchId = e.touchId;
                }
            });
            Laya.stage.on(Laya.Event.MOUSE_UP, this, function (e: Laya.Event) {
                if (this.touchId == e.touchId) {
                    this.isMoving = false;
                    this.controlDirection.pos(this.centerX, this.centerY);
                    this.direction = -4;
                }
            });
            Laya.stage.on(Laya.Event.MOUSE_MOVE, this, function (e: Laya.Event) {
                if (this.touchId == e.touchId) {
                    if (this.isMoving) {
                        let dx: number = Laya.stage.mouseX - this.centerX;
                        let dy: number = Laya.stage.mouseY - this.centerY;
                        this.angle = Math.atan2(dy, dx);
                        let dis = this.getDistance(this.centerX, this.centerY, Laya.stage.mouseX, Laya.stage.mouseY);
                        if (dis > 50) {
                            this.controlDirection.pos(this.centerX + Math.cos(this.angle) * 70, this.centerY + Math.sin(this.angle) * 70);
                        } else {
                            this.controlDirection.pos(Laya.stage.mouseX, Laya.stage.mouseY);
                        }
                        if (Math.abs(dx) > this.directionOffset || Math.abs(dy) > this.directionOffset) {
                            this.direction = this.angle;
                        }
                    } else {

                    }
                }
            });

            this.key1.on(Laya.Event.MOUSE_DOWN, this, function () {
                this.key1Pressed = true;
            });
            this.key1.on(Laya.Event.MOUSE_UP, this, function () {
                this.key1Pressed = false;
            });
            this.key1.on(Laya.Event.MOUSE_OUT, this, function () {
                this.key1Pressed = false;
            });
            this.key2.on(Laya.Event.MOUSE_DOWN, this, function () {
                this.key2Pressed = true;
            });
            this.key2.on(Laya.Event.MOUSE_UP, this, function () {
                this.key2Pressed = false;
            });
            this.key2.on(Laya.Event.MOUSE_OUT, this, function () {
                this.key2Pressed = false;
            });
            this.key3.on(Laya.Event.MOUSE_DOWN, this, function () {
                this.key3Pressed = true;
            });
            this.key3.on(Laya.Event.MOUSE_UP, this, function () {
                this.key3Pressed = false;
            });
            this.key3.on(Laya.Event.MOUSE_OUT, this, function () {
                this.key3Pressed = false;
            });
        } else {
            this.control.visible = false;
            this.controlDirection.visible = false;
            controlPadKeys.visible = false;
        }

        if (this.controlPadType === ControlPadType.Keyboard) {
            Laya.timer.frameLoop(1, this, this.getKeyboardPressed);
        }
    }

    private getKeyboardPressed() {
        if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.A)) {
            this.direction = Math.atan2(0, -1);
        } else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.W)) {
            this.direction = Math.atan2(-1, 0);
        } else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.D)) {
            this.direction = Math.atan2(.01, 1);
        } else if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.S)) {
            this.direction = Math.atan2(1, 0);
        } else {
            this.direction = -4;
        }
        if (Laya.KeyBoardManager.hasKeyDown(Laya.Keyboard.SPACE)) {
            this.key1Pressed = true;
        } else {
            this.key1Pressed = false;
        }
    }

    private getKeyPressed(button: Laya.Sprite) {
        //var pt = button.fromStagePoint(Laya.stage.getMousePoint());
        var pt = button.toParentPoint(new Laya.Point(button.x, button.y));
        if (pt.x > button.x &&
            pt.x < button.x + button.width &&
            pt.y > button.y &&
            pt.y < button.y + button.height) {
            return true;
        }
        // if (Laya.stage.mouseX > button.x &&
        //     Laya.stage.mouseX < button.x + button.width &&
        //     Laya.stage.mouseY > button.y &&
        //     Laya.stage.mouseY < button.y + button.height) {
        //     return true;
        // }
        return false;
    }

    private getDistance(centerX: number, centerY: number, mouseX: number, mouseY: number) {
        let dx: number = centerX - mouseX;
        let dy: number = centerY - mouseY;
        let distance: number = Math.sqrt(dx * dx + dy * dy);
        return distance;
    }
}